package com.xsh136.mvcdemo.controller;

import com.xsh136.mvcdemo.entity.User;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017\7\29 0029.
 */
@Controller
@RequestMapping("/mvc")
@SessionAttributes("user")
public class MVCController {
    @RequestMapping("/index")
    public String hello(Model model){
//        model.addAttribute("mm","你好");
        return "hello";
    }
    @RequestMapping(value = "register",method = RequestMethod.GET)
    public String register(){
        return "register";
    }
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(User user, ModelMap modelMap){
//        session.setAttribute("user",user);
//        modelMap.addFlashAttribute("user",user);
        modelMap.addAttribute("user",user);
        System.out.println(user);
        return "redirect:index";
    }
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public String upload(){
        return "upload";
    }
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(@RequestParam("prefix") String prefix, @RequestParam("file") MultipartFile file) throws IOException {
        String fileName=prefix+file.getOriginalFilename();
        file.transferTo(new File("G:\\upload",fileName));
        return "redirect:list";
    }
    @RequestMapping("/list")
    public String list(ModelMap modelMap){
        File file=new File("G:\\upload");
        String[] fileNames=file.list();
        modelMap.addAttribute("fileNames",fileNames);
        return "list";
    }
//    @RequestMapping("/download")
//    public void download(String fileName, HttpServletResponse response) throws IOException {
//        response.addHeader("Content-Disposition","attachment;filename="+fileName);
//        response.setContentType("application/octet-stream");
//        OutputStream out=response.getOutputStream();
//        InputStream in=new FileInputStream(new File("G:\\upload",fileName));
//        IOUtils.copy(in,out);
//        out.close();
//        in.close();
//    }
@RequestMapping("/download/{fileName}/do")
    public ResponseEntity<FileSystemResource> download(@PathVariable("fileName") String fileName){
        FileSystemResource fsr=new FileSystemResource(new File("G:\\upload",fileName));
        HttpHeaders headers=new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename="+fileName);
        headers.add(HttpHeaders.CONTENT_TYPE,"application/octet-stream");
        return new ResponseEntity<FileSystemResource>(fsr,headers, HttpStatus.OK);
    }

    @RequestMapping("/users")
    @ResponseBody
    public List<User> list(){
        List<User> users=new ArrayList<>();
        users.add(new User("张三","123","zhangsan@qq.com"));
        users.add(new User("李四","456","lisi@qq.com"));
        users.add(new User("王五","789","wangwu@qq.com"));
        return users;
}

 }
